﻿<%--<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Models.TrainingsCourses>" %>--%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Database.Model.TrainingsCourses>" %>
<%@ Import Namespace="CRM.Models" %>
<%@ Import Namespace="System.Drawing" %>

<%
    var typesCol = new CRM.Database.Model.TrainingCoursesTypesCollection(); 
%>

<script type="text/javascript">
//<![CDATA[
    var textSeparator = ";";
    function OnListBoxSelectionChanged3(listBox, args) {
        if (args.index == 0)
            args.isSelected ? listBox.SelectAll() : listBox.UnselectAll();
        UpdateSelectAllItemState3();
        UpdateText3();
    }
    function UpdateSelectAllItemState3() {
        IsAllSelected3() ? checkListBox3.SelectIndices([0]) : checkListBox3.UnselectIndices([0]);
    }
    function IsAllSelected3() {
        for (var i = 1; i < checkListBox3.GetItemCount(); i++)
            if (!checkListBox3.GetItem(i).selected)
                return false;
        return true;
    }
    function UpdateText3() {
        var selectedItems = checkListBox3.GetSelectedItems();
        CourseTypes.SetText(GetSelectedItemsText3(selectedItems));
    }
    function SynchronizeListBoxValues3(dropDown, args) {
        checkListBox3.UnselectAll();
        var texts = dropDown.GetText().split(textSeparator);
        var values = GetValuesByTexts3(texts);
        checkListBox3.SelectValues(values);
        UpdateSelectAllItemState3();
        UpdateText3(); // for remove non-existing texts
    }
    function GetSelectedItemsText3(items) {
        var texts = [];
        for (var i = 0; i < items.length; i++)
            if (items[i].index != 0)
                texts.push(items[i].text);
        return texts.join(textSeparator);
    }
    function GetValuesByTexts3(texts) {
        var actualValues = [];
        var item;
        for (var i = 0; i < texts.length; i++) {
            item = checkListBox3.FindItemByText(texts[i]);
            if (item != null)
                actualValues.push(item.value);
        }
        return actualValues;
    }
// ]]>
</script>

<%:
    Html.DevExpress().Label(
              settings =>
              {
                  settings.ControlStyle.CssClass = "lable";
                  settings.Text = "Список типов курсов";
                  settings.AssociatedControlName = "CourseTypes";
                  settings.Width = 300;

              }).GetHtml() %>

    <%: Html.DevExpress().DropDownEdit(
    settings =>
    {
        settings.Name = "CourseTypes";
        settings.Text = Model.CourseTypes.ToString();
        settings.Width = 300;
        if (Model.IdTrainingsCourse > 0)
            settings.ReadOnly = true;
        settings.Properties.DropDownWindowStyle.BackColor = Color.FromArgb(0xEDEDED);
        settings.SetDropDownWindowTemplateContent(c =>
            {
                Html.DevExpress().ListBox(
                    listBoxSettings =>
                    {
                        listBoxSettings.Name = "checkListBox3";
                        listBoxSettings.SkinID = "CheckComboBoxListBox";
                        if(Model.IdTrainingsCourse > 0)
                                listBoxSettings.Enabled = false;
                        listBoxSettings.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
                        listBoxSettings.Properties.ClientSideEvents.SelectedIndexChanged = "OnListBoxSelectionChanged3";
                        listBoxSettings.ControlStyle.Border.BorderWidth = 0;
                        listBoxSettings.ControlStyle.BorderBottom.BorderWidth = 1;
                        listBoxSettings.ControlStyle.BorderBottom.BorderColor = Color.FromArgb(0xDCDCDC);
                        listBoxSettings.Width = Unit.Percentage(100);
                        listBoxSettings.Properties.DataSourceID = "IdTrainingCoursesType";
                        listBoxSettings.Properties.TextField = "NameRu";
                    }).BindList(typesCol.DropDownList).Bind(Model.CourseTypes).Render();
                    //}).BindList(DB.GetTrainingsCoursesTypesForDropDownList()).Bind(Model.CourseTypes).Render();
                if (!(Model.IdTrainingsCourse > 0))
                {
                    ViewContext.Writer.Write("<table style=\"width:100%\"><tr><td align=\"right\">");
                    Html.DevExpress().Button(buttonSettings =>
                              {
                                  buttonSettings.Name = "buttonClose3";
                                  buttonSettings.Text = "Выбрать";
                                  buttonSettings.Style.Add("float", "right");
                                  buttonSettings.ClientSideEvents.Click = "function(s, e){ CourseTypes.HideDropDown(); }";
                              }).Render();
                    ViewContext.Writer.Write("</td></tr></table>");
                }
            });
        settings.Properties.EnableAnimation = false;
        settings.Properties.ClientSideEvents.TextChanged = "SynchronizeListBoxValues3";
        settings.Properties.ClientSideEvents.DropDown = "SynchronizeListBoxValues3";
    }).Bind(Model.CourseTypes).GetHtml()%>